#DCONFIG_MSPROTOCOL_LIBRTMP 
#-DCONFIG_MSPROTOCOL_TSSL 

D_BASEAPI_FLAGS= \
    -DOS_LINUX_SOC \
    -D__STDC_FORMAT_MACROS \
    -DCONFIG_MSPROTOCOL_SRT \
    -DCONFIG_MSPROTOCOL_MSAV \
    -DCONFIG_MSPROTOCOL_TSSL 
    
D_BASEAPI_FLAGS_SIMPLE= \
    -DOS_LINUX_SOC \
    -D__STDC_FORMAT_MACROS

D_LIBMSLOG_LIBS	:=`pkg-config --libs	libmslog`
D_LIBMSLOG_INCLUDES	:=`pkg-config --cflags	libmslog`
D_LIBMSCOMMON_LIBS	:=`pkg-config --libs	libmscommon`
D_LIBMSCOMMON_INCLUDES	:=`pkg-config --cflags	libmscommon`
D_LIBPROTOCOL_LIBS	:=`pkg-config --libs	libmsprotocol`
D_LIBPROTOCOL_INCLUDES	:=`pkg-config --cflags	libmsprotocol`
#librtmp
D_LIBRTMP_LIBS	:=`pkg-config --libs	librtmp`
D_LIBRTMP_INCLUDES	:=`pkg-config --cflags	librtmp`
#srt
D_SRT_LIBS		:=`pkg-config --libs	haisrt`
D_SRT_INCLUDES	:=`pkg-config --cflags	haisrt`
#openssl
D_OPENSSL_LIBS	:=`pkg-config --libs	openssl`
D_OPENSSL_INCLUDES	:=`pkg-config --cflags	openssl`
#ffmpeg
D_FFMPEG_LIBS=\
	`pkg-config --libs libavcodec` \
	`pkg-config --libs libavfilter` \
	`pkg-config --libs libavformat` \
	`pkg-config --libs libavutil` \
	`pkg-config --libs libavdevice` \
	`pkg-config --libs libswresample` \
	`pkg-config --libs libpostproc` \
	`pkg-config --libs libswscale`
D_FFMPEG_INCLUDES= \
	`pkg-config --cflags libavcodec` \
	`pkg-config --cflags libavfilter` \
	`pkg-config --cflags libavformat` \
	`pkg-config --cflags libavutil` \
	`pkg-config --cflags libavdevice` \
	`pkg-config --cflags libswresample` \
	`pkg-config --cflags libpostproc` \
	`pkg-config --cflags libswscale`

C_BASE_INCLUDE:=${D_LIBMSLOG_INCLUDES} ${D_LIBMSCOMMON_INCLUDES} ${D_FFMPEG_INCLUDES} ${D_SRT_INCLUDES} ${D_OPENSSL_INCLUDES} ${D_BASEAPI_FLAGS} -Isrc 
C_BASE_LIBS   :=${D_LIBMSLOG_LIBS}     ${D_LIBMSCOMMON_LIBS}     ${D_FFMPEG_LIBS}     ${D_SRT_LIBS} ${D_OPENSSL_LIBS}    -pthread -lm 	

C_SIMPLE_INCLUDE:=${D_LIBMSLOG_INCLUDES} ${D_LIBMSCOMMON_INCLUDES}   ${D_BASEAPI_FLAGS_SIMPLE} -Isrc 
C_SIMPLE_LIBS	:=${D_LIBMSLOG_LIBS}     ${D_LIBMSCOMMON_LIBS}        -pthread -lm 

libmsprotocol_CC=${CC}
libmsprotocol_SRC=${C_SRC}
libmsprotocol_CFLAGS=${C_BASE_INCLUDE} ${C_BASE_LIBS} ${CFLAGS_SHARE} -fPIC 

libmsprotocol_simple_CC=${CC}
libmsprotocol_simple_SRC=${C_SRC}
libmsprotocol_simple_CFLAGS=${C_SIMPLE_INCLUDE} ${C_SIMPLE_LIBS} ${CFLAGS_SHARE} -fPIC


#programe
COMMON_CFLAGS=${D_LIBPROTOCOL_LIBS} ${D_LIBPROTOCOL_INCLUDES} ${C_BASE_INCLUDE} ${C_BASE_LIBS}

mscore_mctu_CC=${CC}
mscore_mctu_SRC=${C_SRC} mscore_mctu.c
mscore_mctu_CFLAGS=${COMMON_CFLAGS}

mscore_loadtool_CC=${CC}
mscore_loadtool_SRC=${C_SRC} mscore_loadtool.c
mscore_loadtool_CFLAGS=${COMMON_CFLAGS}

mscore_utm_CC=${CC}
mscore_utm_SRC=${C_SRC} mscore_utm.c
mscore_utm_CFLAGS=${COMMON_CFLAGS}

msptc_client_CC=${CC}
msptc_client_SRC=${C_SRC} msptc_client.c
msptc_client_CFLAGS=${COMMON_CFLAGS}

srtserver_CC=${CC}
srtserver_SRC=${C_SRC} srtserver.c
srtserver_CFLAGS=${COMMON_CFLAGS} 

srtclient_CC=${CC}
srtclient_SRC=${C_SRC} srtclient.c
srtclient_CFLAGS=${COMMON_CFLAGS}

DBG_FLAG=-g -rdynamic

x8664:    libmsprotocol     install mscore_mctu  mscore_loadtool
x8664_gdb:libmsprotocol_gdb install mscore_mctu  mscore_loadtool
simple:   libmsprotocol_simple install

client: msptc_client
srt:srtserver srtclient 
	
libmsprotocol: 
	mkdir -p $(OUT_LIB)
	mkdir -p $(OUT_INCLUDE)
	cp -fr src/*.h $(OUT_INCLUDE)
	$($@_CC)  $($@_SRC)  $($@_CFLAGS)  -o $(OUT_LIB)/$(PROGRAM_NAME).so 
libmsprotocol_gdb: 
	mkdir -p $(OUT_LIB)
	mkdir -p $(OUT_INCLUDE)
	cp -fr src/*.h $(OUT_INCLUDE)
	$(libmsprotocol_CC)  $(libmsprotocol_SRC)  $(libmsprotocol_CFLAGS) ${DBG_FLAG} -o $(OUT_LIB)/$(PROGRAM_NAME).so	
libmsprotocol_simple: 
	mkdir -p $(OUT_LIB)
	mkdir -p $(OUT_INCLUDE)
	cp -fr src/*.h $(OUT_INCLUDE)
	$($@_CC)  $($@_SRC)  $($@_CFLAGS) ${DBG_FLAG} -o $(OUT_LIB)/libmsprotocol.so 
	
sample: mscore_mctu  mscore_loadtool mscore_utm msptc_client srtserver srtclient
mscore_mctu: 
	mkdir -p $(OUT_BIN)
	$($@_CC)  $($@_SRC)  $($@_CFLAGS) ${DBG_FLAG} -o $(OUT_BIN)/$@ 
	cp $(OUT_BIN)/$@ ${prefix_x8664}/bin/mscore_mctu -fr
mscore_loadtool: 
	mkdir -p $(OUT_BIN)
	$($@_CC)  $($@_SRC)  $($@_CFLAGS) ${DBG_FLAG} -o out/bin/$@ 
	cp $(OUT_BIN)/$@ ${prefix_x8664}/bin/mscore_loadtool -fr
mscore_utm: 
	mkdir -p $(OUT_BIN)
	$($@_CC)  $($@_SRC)  $($@_CFLAGS) ${DBG_FLAG} -o out/bin/$@ 
	cp $(OUT_BIN)/$@ ${prefix_x8664}/bin/mscore_utm -fr
msptc_client: 
	mkdir -p $(OUT_BIN)
	$($@_CC)  $($@_SRC)  $($@_CFLAGS) ${DBG_FLAG} -o out/bin/$@
srtserver: 
	mkdir -p $(OUT_BIN)
	$($@_CC)  $($@_SRC)  $($@_CFLAGS) ${DBG_FLAG} -o out/bin/$@ 
srtclient: 
	mkdir -p $(OUT_BIN)
	$($@_CC)  $($@_SRC)  $($@_CFLAGS) ${DBG_FLAG} -o out/bin/$@ 

install:
	mkdir -p  ${prefix_x8664}/lib/ 
	mkdir -p  ${prefix_x8664}/include/   
	mkdir -p  ${prefix_x8664}/lib/pkgconfig/ 
	cp -fr $(OUT_LIB)/libms*.so		${prefix_x8664}/lib/     
	cp -fr $(OUT_INCLUDE)			${prefix_x8664}/include/	     
	cp -fr platform/$(PROGRAM_NAME).pc	${prefix_x8664}/lib/pkgconfig/
	ldconfig
uninstall:
	rm -fr ${prefix_x8664}/lib/$(PROGRAM_NAME).so 
	rm -fr ${prefix_x8664}/include/$(PROGRAM_NAME)
	rm -fr ${prefix_x8664}/lib/pkgconfig/$(PROGRAM_NAME).pc
	rm -fr ${prefix_x8664}/bin/mscore_mctu
	rm -fr ${prefix_x8664}/bin/mscore_test_concurrent
